package com.dingding.service.activity.universal.core.aopconfig;

import org.apache.log4j.Logger;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

import com.dingding.common.core.base.dto.FacadeResult;
import com.dingding.common.core.exception.bussiness.BussinessException;
import com.dingding.common.core.exception.server.ServerException;

/**
 * @Description 异常拦截
 * @author fengshuonan
 * @date 2016年12月7日 下午4:15:04
 */
@Aspect
@Component
public class ExceptionAop {
	
	private Logger log = Logger.getLogger(this.getClass());
	
	@Pointcut("execution(* com.dingding.service.activity.universal.CommonActivityFacadeImpl.*(..))")
	public void cutService() {
	}
	
	@Around("cutService()")
	public Object exceptionAop(ProceedingJoinPoint point) throws Throwable {
		
		@SuppressWarnings("rawtypes")
		FacadeResult facadeResult = new FacadeResult();
		
		try {
			return point.proceed();
		} catch (BussinessException e) {
			facadeResult.setCode(e.getCode());
			facadeResult.setMessage(e.getErrorMessage());
		} catch (ServerException e) {
			log.error("拦截到数据库异常！",e);
			facadeResult.setCode(e.getCode());
			facadeResult.setMessage(e.getErrorMessage());
		} catch (Throwable e) {
			log.error("拦截到其他异常！",e);
			facadeResult.setCode(999);
			facadeResult.setMessage("网络异常");
		}
		return facadeResult;
	}
}